package com.testmanager.testmananger.user.web;




import com.testmanager.testmananger.foundation.util.JWTUtil;
import com.testmanager.testmananger.foundation.dto.ConstantDto;
import com.testmanager.testmananger.user.service.ConstantService;
import com.testmanager.testmananger.user.service.UserService;
import com.testmanager.testmananger.user.domain.UserEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserResource {

    @Autowired
    UserService userService;
    @Autowired
    ConstantService constantService;

    @PostMapping("savenewemployee")
    public UserEntity saveUser(@RequestBody UserEntity userEntity){
        log.info(userEntity.toString());
        return userService.saveUser(userEntity);
    }

    @GetMapping("getemployees")
    public List<UserEntity> findAllUser(){
        return userService.getAllUser();
    }

    @GetMapping("getUserJobType")
    public List<ConstantDto> getUserJobType(){return constantService.getUserJobType();
    }
    @GetMapping("testlong")
    public long getLongTest(@RequestParam(defaultValue = "0",required = false) Long userid){
   // log.info(userid.toString());
    return userid;
    }

    @Autowired
    StringRedisTemplate template ;
    @GetMapping("redisSet")
    public String getAndSetRedis(@RequestParam("key") String key,@RequestParam("value") String value){
       template.opsForValue().set(key,value);
       return  template.opsForValue().get(key);
        // http://127.0.0.1:8081/user/redisSet?key=redisset&value=123

    }


    @GetMapping("/getToken")
    public ResponseEntity getToken(){
        String sign = JWTUtil.sign("何森存","hsc", 125332);
        return ResponseEntity.ok(sign);

    }

    @GetMapping("/getUserid")
    public ResponseEntity getUserid(@RequestParam("token") String token ){
        long userid = 0 ;
        if (JWTUtil.verity(token)){
            userid = JWTUtil.getUserid(token);
        }

        return ResponseEntity.ok(userid);
    }

}
